package com.lenovo.lcdm.team.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lenovo.lcdm.common.enums.DeleteFlagEnum;
import com.lenovo.lcdm.common.enums.ResponseMsgEnum;
import com.lenovo.lcdm.common.model.ResponseVo;
import com.lenovo.lcdm.team.common.util.ContextUtil;
import com.lenovo.lcdm.team.dto.UserDto;
import com.lenovo.lcdm.team.dto.UserPwdDto;
import com.lenovo.lcdm.team.dto.UserQueryDto;
import com.lenovo.lcdm.team.entity.TeamUser;
import com.lenovo.lcdm.team.service.ITeamUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/team")
public class UserController {

    @Autowired
    private ITeamUserService userService;


    @GetMapping("/user/list")
    public ResponseVo<Page<TeamUser>> userListByPage(@RequestParam int pageNo, @RequestParam int pageSize, @RequestParam(required = false) String itCode, @RequestParam(required = false) String username) {
        Page<TeamUser> page = new Page<>(pageNo, pageSize);
        UserQueryDto userDto = new UserQueryDto();
        if(StringUtils.isNotEmpty(itCode)){
            userDto.setItCode(itCode);
        }
        if(StringUtils.isNotEmpty(username)){
            userDto.setUserName(username);
        }
        List<TeamUser> data = userService.listByPage(page, userDto);
        page.setRecords(data);
        ResponseVo<Page<TeamUser>> responseVo = new ResponseVo<>();
        responseVo.setData(page);
        return responseVo;
    }

    @GetMapping("/users")
    public ResponseVo<List<TeamUser>> getUsers(@RequestParam String itCode, @RequestParam(required = false) Long countLimit) {
        LambdaQueryWrapper<TeamUser> userQw = new LambdaQueryWrapper<>();
        userQw.like(TeamUser::getItCode, itCode).eq(TeamUser::getDeleted, DeleteFlagEnum.NO.getValue());
        if(countLimit != null) {
            userQw.last(" limit " + countLimit);
        }
        List<TeamUser> data = userService.list(userQw);
        ResponseVo<List<TeamUser>> responseVo = new ResponseVo<>();
        responseVo.setData(data);
        return responseVo;
    }

    @PostMapping("/user")
    public ResponseVo saveUser(@RequestBody UserDto userDto) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            if(userDto.getId() == null){
                userDto.setCreator(ContextUtil.getCurrentUser());
            }
            userDto.setModifier(ContextUtil.getCurrentUser());
            userService.saveUser(userDto);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @DeleteMapping("/user")
    public ResponseVo deleteUser(@RequestParam("itCode") String itCode) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            userService.deleteUser(itCode);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @GetMapping("/user/{id}")
    public ResponseVo<UserDto> getUser(@PathVariable("id") Long id) {
        ResponseVo<UserDto> responseVo = new ResponseVo<>();
        try {
            UserDto user = userService.getUserInfo(id);
            responseVo.setData(user);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @PutMapping("/user/password")
    public ResponseVo modifyPwd(@RequestBody UserPwdDto userPwdDto) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            responseVo.setData(userService.modifyPwd(userPwdDto, false));
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @PutMapping("/user/password-reset")
    public ResponseVo resetPwd(@RequestBody UserPwdDto userPwdDto) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            ;
            responseVo.setData(userService.modifyPwd(userPwdDto, true));
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

}
